package viewcontroller;

import utils.Category;
import utils.HorizontalListView;
import utils.ServiceHandler;

import com.example.lastprojectbeautiplus.R;
import com.google.gson.Gson;

import adapter.HorizontalListViewAdapter;
import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;

public class CategoryViewFragment extends Fragment {
	public CategoryViewFragment() {
	}
	private SQLiteDatabase dbTarget;
	private Cursor csTarget;
	@SuppressWarnings("unused")
	private HorizontalListView hrzType1, hrzType2, hrzType3, hrzType4,
			hrzType5, hrzType6, hrzType7, hrzType8;
	private HorizontalListViewAdapter hrzAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.fragment_target_view,
				container, false);
		createTargetTable();
		csTarget = dbTarget.rawQuery("SELECT * FROM TableTarget", null);
		csTarget.moveToFirst();
		int target = csTarget.getInt(csTarget.getColumnIndex("target"));
		int height;
		DisplayMetrics metrics = getResources().getDisplayMetrics();
		height = metrics.heightPixels;
		HorizontalListView[] hrzType = new HorizontalListView[8];
		hrzType[0] = hrzType1 = (HorizontalListView) rootView
				.findViewById(R.id.hrz1);
		hrzType[1] = hrzType2 = (HorizontalListView) rootView
				.findViewById(R.id.hrz2);
		hrzType[2] = hrzType3 = (HorizontalListView) rootView
				.findViewById(R.id.hrz3);
		hrzType[3] = hrzType4 = (HorizontalListView) rootView
				.findViewById(R.id.hrz4);
		hrzType[4] = hrzType5 = (HorizontalListView) rootView
				.findViewById(R.id.hrz5);
		hrzType[5] = hrzType6 = (HorizontalListView) rootView
				.findViewById(R.id.hrz6);
		hrzType[6] = hrzType7 = (HorizontalListView) rootView
				.findViewById(R.id.hrz7);
		hrzType[7] = hrzType8 = (HorizontalListView) rootView
				.findViewById(R.id.hrz8);

		int begin = 0, end = 0;
		if (target == 1) {
			begin = 1;
			end = 6;
		} else if (target == 2) {
			begin = 7;
			end = 14;
		} else if (target == 3) {
			begin = 15;
			end = 19;
		}
		String urlHrz = null;
		for (int i = 0; i <= end - begin; i++) {
			urlHrz = "http://services-beautyplus.rhcloud.com/rest/ProductServices/Categories?typeID="
					+ (i + begin);
			hrzType[i].getLayoutParams().height = height * 2 / 7;
			new GetCategoryList(urlHrz, hrzType[i]).execute();
		}
		return rootView;
	}

	private class GetCategoryList extends AsyncTask<Void, Void, Void> {
		private String url;
		private String jsonType;
		private HorizontalListView hrz;

		public GetCategoryList(String url, HorizontalListView hrz) {
			this.url = url;
			this.hrz = hrz;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			// Creating service handler class instance
			ServiceHandler sh = new ServiceHandler();
			// Making a request to url and getting response
			jsonType = sh.executeGetRequest(url);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			Gson myGson = new Gson();
			final Category[] listCategory = myGson.fromJson(jsonType,
					Category[].class);
			hrzAdapter = new HorizontalListViewAdapter(getActivity(),
					listCategory);
			hrz.setAdapter(hrzAdapter);
			hrz.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> parent, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(getActivity(), SlideMenu.class);
					intent.putExtra("FromCateView",
							listCategory[position].getCategoryID());
					getActivity().startActivity(intent);
				}

				@Override
				public void onNothingSelected(AdapterView<?> parent) {
					// TODO Auto-generated method stub

				}
			});
		}
	}
	private void createTargetTable() {
		dbTarget = getActivity().openOrCreateDatabase("TableTarget",
				android.content.Context.MODE_PRIVATE, null);
		String sqlTarget = "CREATE TABLE IF NOT EXISTS TableTarget(target INT);";
		dbTarget.execSQL(sqlTarget);
	}
}
